package com.grape.dp.strategy.context;

import com.grape.dp.strategy.entity.PaymentChannelEntity;
import com.grape.dp.strategy.inter.PayStrategy;
import com.grape.dp.strategy.mapper.PaymentChannelMapper;
import com.grape.dp.strategy.utils.SpringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/**
 * 描述:PayContextStrategy 上下文
 * @author: myx
 * @date: 2019-07-21
 * Copyright © 2019-grape. All rights reserved.
 */
@Component
public class PayContextStrategy {
    @Autowired
    private PaymentChannelMapper paymentChannelMapper;
    @Autowired
    private SpringUtils springUtils;
    public String toPayHtml(String payCode){
         //1.使用payCode参数查询数据库获取beanid
        PaymentChannelEntity paymentChannel = paymentChannelMapper.getPaymentChannel(payCode);
        if(paymentChannel==null){
            return  "没有该渠道信息";
        }
        //2.获取到bean的id之后，使用spring容器获取实例对象
        String strategyBeanId = paymentChannel.getStrategyBeanId();
        if(StringUtils.isEmpty(strategyBeanId)){
            return  "该渠道没有配置beanid";
        }
        // 3.执行该实现的方法即可.... aliPayStrategy
        PayStrategy payStrategy=SpringUtils.getBean(strategyBeanId, PayStrategy.class);
        return  payStrategy.toPayHtml();
     }
     // 优化 支付渠道存放内存中...
}
